package libtool65.o65;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;

public class Option {
//	   .byt olen		; overall length (including length and type byte
	int length = 0;
//	   .byt otype		; option type
	int type = -1;
//	   [ .byt option_bytes ]
	byte[] data = null;
	
	public void read(InputStream file) throws IOException, OptionException {
		length = file.read();
		if (length > 0)
		{
			type = file.read();
			data = new byte [length-2];
			int bytes_read = file.read(data);
			if (bytes_read != length-2)
				throw new OptionException(length, type);
		}
	}

	public boolean isEmpty() {
		return length == 0;
	}

	public void dumpInfo(PrintStream out) {
		out.print("OPTION ");
		switch (type)
		{
		case 0:	out.println("FILENAME("+getText()+")"); break;
		case 1:	out.println("OSHEADER("+getOsHeader()+")"); break;
		case 2:	out.println("ASSEMBLER("+getText()+")"); break;
		case 3:	out.println("AUTHOR("+getText()+")"); break;
		case 4:	out.println("DATE("+getText()+")"); break;
		default: out.println("type="+type+" length="+length); break;
		}
	}

	public String getOsHeader() {
		String os_string = null;
		switch(data[0])
		{
		case 0: os_string = "none"; break;
		case 1: os_string = "OSA/65"; break;
		case 2: os_string = "Lunix"; break;
		case 3: os_string = "cc65"; break;
		case 4: os_string = "opencbm"; break;
		default: os_string = "unknown/"+data[0]; break;
		}
		return os_string+",dlen="+data.length;
	}

	public String getText() {
		return new String(data, 0, data.length-1);
	}
}
